<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateShopTables extends Migration
{
    public function getConnection()
    {
        return $this->config('database.connection') ?: config('database.default');
    }

    public function config($key)
    {
        return config('shop.'.$key);
    }

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create($this->config('database.users_table'), function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('shop_id')->default(0)->comment("门店ID");
            $table->integer('agent_id')->default(0)->comment("代理商ID");
            $table->integer('user_id')->default(0)->comment("绑定用户ID");
            $table->integer('cid')->default(0)->comment('分类ID');
            $table->string('name')->default('')->comment('门店名称');
            $table->string('fullname')->nullable()->comment('门店负责人');
            $table->string('avatar', 120)->nullable()->comment("门店图片");
            $table->string('phone')->default('')->comment('门店电话');
            $table->string('username', 120)->unique();
            $table->string('password', 80);
            $table->string('remember_token', 100)->nullable();
            $table->string('address')->nullable()->comment('所在地址');
            $table->string('longitude',50)->nullable()->comment("经度");
            $table->string('latitude',50)->nullable()->comment("纬度");
            $table->integer('province_id')->default(0)->comment('省ID');
            $table->integer('city_id')->default(0)->comment('市ID');
            $table->integer('district_id')->default(0)->comment('区ID');
            $table->json('images')->nullable()->comment('门店图片组');
            $table->decimal('shop_rate')->default(0.00)->comment("门店服务每笔交易分成比例");
            $table->decimal('card_rate')->default(0.00)->comment("门店洗车卡每笔交易分成比例");
            $table->decimal('money')->default(0.00)->comment("可提现余额");
            $table->decimal('used_money')->default(0.00)->comment("已提现余额");
            $table->integer('distance')->default(0)->comment("距离公里设置");
            $table->decimal('distance_price')->default(0.00)->comment("超出限制收费(每公里)");

            $table->tinyInteger('status')->default("0")->comment('状态:0=正常,1=隐藏');
            $table->timestamps();
        });

        Schema::create($this->config('database.roles_table'), function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name', 50);
            $table->string('slug', 50)->unique();
            $table->timestamps();
        });

        Schema::create($this->config('database.permissions_table'), function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name', 50);
            $table->string('slug', 50)->unique();
            $table->string('http_method')->nullable();
            $table->text('http_path')->nullable();
            $table->integer('order')->default(0);
            $table->bigInteger('parent_id')->default(0);
            $table->timestamps();
        });

        Schema::create($this->config('database.menu_table'), function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('parent_id')->default(0);
            $table->integer('order')->default(0);
            $table->string('title', 50);
            $table->string('icon', 50)->nullable();
            $table->string('uri', 50)->nullable();
            $table->tinyInteger('show')->default(1);
            $table->string('extension', 50)->default('');
            $table->timestamps();
        });

        Schema::create($this->config('database.role_users_table'), function (Blueprint $table) {
            $table->bigInteger('role_id');
            $table->bigInteger('user_id');
            $table->unique(['role_id', 'user_id']);
            $table->timestamps();
        });

        Schema::create($this->config('database.role_permissions_table'), function (Blueprint $table) {
            $table->bigInteger('role_id');
            $table->bigInteger('permission_id');
            $table->unique(['role_id', 'permission_id']);
            $table->timestamps();
        });

        Schema::create($this->config('database.role_menu_table'), function (Blueprint $table) {
            $table->bigInteger('role_id');
            $table->bigInteger('menu_id');
            $table->unique(['role_id', 'menu_id']);
            $table->timestamps();
        });

        Schema::create($this->config('database.permission_menu_table'), function (Blueprint $table) {
            $table->bigInteger('permission_id');
            $table->bigInteger('menu_id');
            $table->unique(['permission_id', 'menu_id']);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists($this->config('database.users_table'));
        Schema::dropIfExists($this->config('database.roles_table'));
        Schema::dropIfExists($this->config('database.permissions_table'));
        Schema::dropIfExists($this->config('database.menu_table'));
        Schema::dropIfExists($this->config('database.role_users_table'));
        Schema::dropIfExists($this->config('database.role_permissions_table'));
        Schema::dropIfExists($this->config('database.role_menu_table'));
        Schema::dropIfExists($this->config('database.permission_menu_table'));
    }
}
